package com.edge.pbx.view.summing;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;

import com.edge.pbxdsp.R;
import com.hanlink.dsp.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

import static com.hanlink.dsp.util.DataUtil.KEY_CHANNEL_NAME;
import static com.hanlink.dsp.util.DataUtil.KEY_INPUT;

/**
 * Copyright (C), 深圳市东乘电子科技有限公司
 * Carate By YangZhong
 */
public class SummingInputLayout extends ViewGroup implements View.OnClickListener, View.OnLongClickListener {
    private List<ImageButton> mImageButtons;
    private float mHpaddingScale = 0.05f;
    private boolean isClickable = true;
    private final PbxInputChannelView[] mPbxInputChannelViews;
    public SummingInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPbxInputChannelViews = new PbxInputChannelView[8];
        mImageButtons = new ArrayList<>();
        mHpaddingScale = context.getResources().getInteger(R.integer.pbx_summing_outputselect_bg_margin_top)/100f;
    }

    private int mTopViewHeight ;
    private int mHPadding, mVPadding;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);

        //2,3,3,3,3
        mHPadding = (int) (width*0.05f);
        mVPadding = (int)(height * 0.03f);
        mTopViewHeight = (int) (height*0.1f);
        int itemWidth = (width - mHPadding*3)/2;
        int itemHeight = (height - mVPadding*10 - mTopViewHeight) /4;
        View view = null;
        view = getChildAt(0);
        view.measure(MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(mTopViewHeight, MeasureSpec.EXACTLY));
        for (int i = 1, count = getChildCount(); i < count; i++){
            view = getChildAt(i);
            view.setClickable(true);
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);
            view.measure(MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(itemHeight, MeasureSpec.EXACTLY));
        }

        setMeasuredDimension(width, height);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        View view = null;
        view = getChildAt(0);
        view.layout(0,mVPadding,view.getMeasuredWidth(),mVPadding+ view.getMeasuredHeight());
        int left, top;
        for (int i = 0, count = getChildCount()-1; i < count; i++) {
            view = getChildAt(i+1);
            top = mTopViewHeight + mVPadding*2 + (i/2)*(view.getMeasuredHeight()+mVPadding*2);
            left = (i%2)*(view.getMeasuredWidth() + mHPadding) + mHPadding;
            view.layout(left, top, left+view.getMeasuredWidth(), top+view.getMeasuredHeight());

        }
    }
    public void setSelectChs(int[] high){
        if (high == null)return;

        for (int i = 0, len = Math.min(high.length, mPbxInputChannelViews.length); i < len; i++){
            mPbxInputChannelViews[i].setSelected(high[i] == 1);
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int ids[] = {
                R.id.summing_input_ch1, R.id.summing_input_ch2,
                R.id.summing_input_ch3, R.id.summing_input_ch4,
                R.id.summing_input_ch5, R.id.summing_input_ch6,
                R.id.summing_input_ch7, R.id.summing_input_ch8
        };
        int i = 0;
        PbxInputChannelView view = null;
        String name = null;
        for (int id : ids) {
            view = findViewById(id);
            name = SPUtils.getInstance(Context.MODE_PRIVATE).getString(KEY_INPUT+i, null);
            if (TextUtils.isEmpty(name))
                name = "CH " +(i+1);
            view.setName( name);
            mPbxInputChannelViews[i] =view;
            ++i;
        }

    }

    @Override
    public void onClick(View v) {
        if (!isClickable)return;
        int index = -1;
        switch (v.getId()){
            case R.id.summing_input_ch1:
                index = 0;
                break;
            case R.id.summing_input_ch2:
                index = 1;
                break;
            case R.id.summing_input_ch3:
                index = 2;
                break;
            case R.id.summing_input_ch4:
                index = 3;
                break;
            case R.id.summing_input_ch5:
                index = 4;
                break;
            case R.id.summing_input_ch6:
                index = 5;
                break;
            case R.id.summing_input_ch7:
                index = 6;
                break;
            case R.id.summing_input_ch8:
                index = 7;
                break;
        }
        if (index != -1){
            v.setSelected(!v.isSelected());
            if (mOnInputLayoutListener != null){
                mOnInputLayoutListener.onInputClick(index, v.isSelected());
            }
        }
    }

    public void resetSelected(){
        View view = null;
        for(int i  = 0, len = getChildCount();  i < len ; i++){
            view = getChildAt(i);
            if (view.isSelected())
                view.setSelected(false);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        isClickable = clickable;
        View view = null;
        for(int i  = 0, len = getChildCount();  i < len ; i++){
            view = getChildAt(i);
            view.setEnabled(clickable);
        }

    }

    private OnInputLayoutListener mOnInputLayoutListener;

    public void setOnInputLayoutListener(OnInputLayoutListener onInputLayoutListener) {
        mOnInputLayoutListener = onInputLayoutListener;
    }

    @Override
    public boolean onLongClick(View v) {
        if (!isClickable)return false;
        int index = -1;
        switch (v.getId()){
            case R.id.summing_input_ch1:
                index = 0;
                break;
            case R.id.summing_input_ch2:
                index = 1;
                break;
            case R.id.summing_input_ch3:
                index = 2;
                break;
            case R.id.summing_input_ch4:
                index = 3;
                break;
            case R.id.summing_input_ch5:
                index = 4;
                break;
            case R.id.summing_input_ch6:
                index = 5;
                break;
            case R.id.summing_input_ch7:
                index = 6;
                break;
            case R.id.summing_input_ch8:
                index = 7;
                break;
        }
        ViewUtil.showChangeInputDialog(getContext(), index, name->{
            ((PbxInputChannelView)v).setName(name);
        });
        return false;
    }

    public interface OnInputLayoutListener{
        void onInputClick(int index, boolean isSelected);
    }
}
